package com.honey.solr.entity;

import java.util.List;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.Clazz;
import com.honey.compilation.java.Field;
import com.honey.compilation.java.InnerClass;
import com.honey.compilation.java.InnerEnum;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.StaticBlock;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.ContentType;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.generator.java.extension.point.GeneratorJavaClass;
import com.honey.solr.SchemaField;
import com.honey.solr.SolrFactory;
import com.honey.solr.entity.method.ConstructorMethod;
import com.honey.solr.entity.method.DefaultConstructorMethod;
import com.honey.solr.entity.method.SetterAndGetterMethod;
import com.honey.solr.entity.method.ToStringMethod;

public class SolrEntity implements  GeneratorJavaClass{
	
	private SolrFactory solrFactory = null;
	
	public SolrEntity(SolrFactory solrFactory){
		this.solrFactory = solrFactory;
	}
	private Clazz solrEntity = null;
	
	@Override
	public AbstractJavaCompilation createNewCompilation(
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		try {
			solrEntity = new Clazz(solrFactory.getEntityType());
			if(solrFactory.getSolrPlugin().isTargetEntitySerializable()){
				solrEntity.addSerializable();
			}
			
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return solrEntity;
	}
	
	@Override
	public Field[] importFields() {
		List<SchemaField> list = solrFactory.getSchemaFields() ;
		Field[] answer = new Field[list.size()];
		for(int i=0,size = list.size();i<size;i++ ){
			SchemaField sf =  list.get(i);
			answer[i] = new Field(solrFactory.getJavaNameCalculator().fieldNameCalculator( sf.getName()), sf.getType());
			answer[i].addAnnotation("Field");
			answer[i].addJavaDocLine(sf.getComment());
		}
		
		return answer;
	}

	@Override
	public FullyQualifiedJavaType[] importImportClass() {
		return new FullyQualifiedJavaType[]{
			new FullyQualifiedJavaType("org.apache.solr.client.solrj.beans.Field"),
		};
	}

	@Override
	public InnerClass[] importInnerClass() {
		
		return null;
	}

	@Override
	public InnerEnum[] importInnerEnumClass() {
		
		return null;
	}

	@Override
	public Method[] importMethods() {
		Method[] answer = new Method[3 + solrFactory.getSchemaFields().size()*2]; 
		answer[0] = (new DefaultConstructorMethod(solrFactory)).createMethod();
		answer[1] = (new ConstructorMethod(solrFactory)).createMethod();
		List<Method> list = new SetterAndGetterMethod(solrFactory,solrEntity).createMethod();
		for(int i=0,size = list.size();i<size;i++ ){
			answer[i+2] =list.get(i);
		}
		answer[answer.length -1 ]  =  new ToStringMethod(solrFactory).createMethod() ;
		return answer;
	}

	@Override
	public StaticBlock[] importStaticBlocks() {
		
		return null;
	}
	
	@Override
	public ContentType getContentType() {
		
		return ContentType.JAVA_ENTITY_BEEN;
	}

	@Override
	public FullyQualifiedJavaType getType() {
		
		return solrEntity.getType();
	}
}
